#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;

//学生节点
class Node
{
private:
    //将信息设置为私有变量更加安全，需要专门接口函数进行查询
    string id;  //学号
    string name;  //姓名
public:
    Node* next;
    Node* pre;
    //构造函数
    Node(string id1, string name1, Node* nextval = NULL, Node* preval = NULL)
    {
        id = id1;
        name = name1;
        next = nextval;
        pre = preval;
    }
    Node(Node* nextval = NULL, Node* preval = NULL)
    {
        next = nextval;
        pre = preval;
    }
    string getId()
    {
        return this->id;
    }
    string getName()
    {
        return this->name;
    }
    void setName(string s)
    {
        this->name = s;
    }
    void print()
    {
        cout << "Node{id = " << this->id << ",  name = " << this->name << "}" << endl;
    }
};

//链表类
class DoubleLink
{
private:
    //头结点
    Node* Head = new Node();
public:
    //添加节点
    void add(Node* node)
    {
        Node* temp = Head;
        while (true)
        {
            if (temp->next == NULL)
            {
                break;
            }
            temp = temp->next;
        }
        //双向链接
        temp->next = node;
        node->pre = temp;
    }

    //删除指定学号学生
    void del(string s)
    {
        Node* temp = Head->next;
        if (temp->next == NULL)
        {
            cout << "The list is empty!" << endl;
            return;
        }

        int flag = 0;
        while (true)
        {
            if (temp == NULL)
            {
                break;
            }
            if (temp->getId() == s)
            {
                flag = 1;
                break;
            }
            temp = temp->next;
        }

        if (!flag)
        {
            cout << "No match found!" << endl;
            return;
        }

        //如果temp是不是最后一个，才可以执行该操作
        if (temp->next != NULL)
        {
            temp->next->pre = temp->pre;
        }
        temp->pre->next = temp->next;
    }

    //查找指定学号学生
    void find(string s)
    {
        if (Head->next == NULL)
        {
            cout << "The list is empty!" << endl;
            return;
        }
        Node* temp = Head;
        int flag = 0;
        while (temp)
        {
            if (temp->next == NULL)
            {
                break;
            }
            if (temp->next->getId() == s)
            {
                flag = 1;
                break;
            }
            temp = temp->next;
        }
        if (!flag)
        {
            cout << "This student is not exist!" << endl;
            return;
        }
        cout << "This student is exist! "  << endl;
    }

    //修改指定学生信息
    void update(Node* newnode)
    {
        if (Head->next == NULL)
        {
            cout << "The list is empty!" << endl;
            return;
        }
        Node* temp = Head;
        int flag = 0;
        while (temp)
        {
            if (temp->next == NULL)
            {
                break;
            }
            if (temp->next->getId() == newnode->getId())
            {
                flag = 1;
                break;
            }
            temp = temp->next;
        }
        if (!flag)
        {
            cout << "This student is not exist!" << endl;
            return;
        }
        temp->next->setName(newnode->getName());
    }
    //正向打印链表所有节点信息
    void list()
    {
        if (Head->next == NULL)
        {
            cout << "The list is empty!" << endl;
            return;
        }
        Node* temp = Head->next;
        cout << "list:" << endl;
        while (temp)
        {
            temp->print();
            temp = temp->next;
        }
    }
    //反向打印链表所有节点信息
    void traverseList()
    {
        if (Head->next == NULL)
        {
            cout << "The list is empty!" << endl;
            return;
        }
        Node* temp = Head->next;
        cout << "traverseList:" << endl;
        //找到最后一个节点
        while (1)
        {
            if (temp->next == NULL)
            {
                break;
            }
            temp = temp->next;
        }
        //反向打印
        while (temp != Head)
        {
            temp->print();
            temp = temp->pre;
        }
    }

};

int main()
{
    Node* stu1 = new Node("202100", "amy");
    Node* stu2 = new Node("202101", "Jone");
    Node* stu3 = new Node("202102", "zc");
    Node* newnode = new Node("202101", "timi");

    DoubleLink link = DoubleLink();
    link.list();
    link.add(stu1);
    link.add(stu2);
    link.add(stu3);
    cout << "增加三名成员：" << endl;
    link.list();
    link.del("202100");
    cout << "删除第一名成员：" << endl;
    link.list();
    cout << "寻找第二名成员：" << endl;
    link.find("202101");
    cout << "修改成员信息：" << endl;
    link.update(newnode);
    link.list();
    cout << "反向输出：" << endl;
    link.traverseList();

    system("pause");
    return 0;

}